/**
 *  表单验证插件
 * */
let Validate = function(Vue ,options){
    Vue.directive('vali', {
        bind: function (el, binding, vnode) {
            let vm = vnode.context//vue
            //获取相应规则
            let rules = binding.value;
            check.rules.push(rules);//验证规则列表
            check.forms.push(el);//需要验证的表单列表
            vm.$vali = check;//验证对象，给使用页面使用
        }
    });
}

let check = {
    forms:[],
    rules:[],
    checkAll(){//验证全部
        let first = false;
        let result = true;
        for (let index = 0; index < this.forms.length; index++) {
            if( !this.checkOne( this.rules[index] , this.forms[index] )){//逐个验证，传入验证规则 和 对象 返回验证结果
                result = false ;
            }
            if(!first && !result) //判断是否有验证失败的！没有则设置 first
                first = this.forms[index];
        }
        if(first) first.focus();//执行first 的 focus 事件获得输入焦点，提示用户输入
        return result;
    },
    checkOne(rules,item){//单个验证
        let result = true;
        for (let index = 0; index < rules.length; index++) {
            if(rules[index].type == 'required' && isEmpty(item,rules[index].msg) ){//空判断
                result = false;
            }
            if(rules[index].type == 'reg' && checkReg(rules[index],item) ){//正则验证
                result = false;
            }
            if(rules[index].type == 'range' && isRange(rules[index],item) ){//数字值范围
                result = false;
            }
        }
        return result;
    }
}

//验证空
function isEmpty(item,msg){
    if(msg == undefined) msg = '不能为空';
    let reg = /(^\s+)|(\s+$)/g;
    let result =  (reg.test(item.value) || item.value == '') ?true:false;
    if(result){
        item.nextElementSibling.innerHTML = msg;
    }else
        item.nextElementSibling.innerHTML = '';
    return result;
}

function isRange(rule,item){
    let range = rule.range
    let min = range[0];
    let max = range[1];
    let len = item.value;

    let result = false;    
    if(isNaN(len) || len == '' && (rule.empty == undefined || rule.empty == false) ){
        item.nextElementSibling.innerHTML =  "请正确输入数字";
        result = true;
    }else if (min != undefined && max != undefined ) {
        if (len < min || len > max) {
            item.nextElementSibling.innerHTML =  "数值应在" + min + "与" + max + "之间";
            result = true;
        } else {
            item.nextElementSibling.innerHTML = '';
        }
    } else if (min != undefined) {
        if (len < min) {
            item.nextElementSibling.innerHTML =  "数值应大于" + min;
            result = true;
        } else {
            item.nextElementSibling.innerHTML = '';
        }
    } else if (max != undefined) {
        if (len > max) {
            item.nextElementSibling.innerHTML =  "数值应小于" + min;
            result = true;
        } else {
            item.nextElementSibling.innerHTML = '';
        }
    }
    return result;
}

function checkReg(test,item){
    let result = false; 
    let regList = {
        english:{reg:/^[0-9a-zA-Z\_]+$/,msg: "只允许输入英文字母、数字、_"}
    };
    let msg = test.msg;
    if(msg == undefined && regList[test.name] != undefined ) msg = regList[test.name].msg;
    if( test.empty && item.value == '' ){//允许空
        item.nextElementSibling.innerHTML =  "";
        result = false;
    }else if( test.reg != undefined ){//自定义正则
        result = (test.reg.test(item.value)) ?false:true;
    }else if(regList[test.name]){//内置正则
        result = (regList[test.name].reg.test(item.value)) ?false:true;
    }else{
        console.log('无法识别的验证');
        result = true;
        msg = '无法识别的验证';
    }
    if(result){
        item.nextElementSibling.innerHTML =  msg;
    }else
        item.nextElementSibling.innerHTML = '';
    return result;
}

//服务器验证
function checkServer(){

}

//自定义函数验证
function checkFun(){
    
}

export default {
    install:Validate
}